package com.ruoyi.project.pro.style.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.cms.CmsNotDeleteException;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.pro.style.domain.ProStyle;
import com.ruoyi.project.pro.style.service.IStyleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: zhizun
 * @author: Mrs.ye
 * @create: 2020-09-16 20:38
 * @description:
 * 产品系列管理控制器
 **/

@Controller
@RequestMapping("/pro/style")
public class ProStyleController extends BaseController {
    @Autowired
    private IStyleService styleService;

    //路径前缀
    private String prefix = "pro/style";

    /**
     *@return 跳转到展示产品风格的html页面
     */
    @RequiresPermissions("pro:style:view")
    @GetMapping()
    public String content() {
        return prefix + "/style";
    }
   /* *
     *@return 跳转到添加产品风格的html页面
     */
    @RequiresPermissions("pro:style:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改产品风格时的数据回显操作
     * @param styleId
     * @param map
     * @return
     * 1)根据id到数据库中查询对应的id,并保存在modelMap中
     * 2)跳转到修改的页面,进行数据回显
     *
     */
    @GetMapping("/edit/{styleId}")
    public String edit(@PathVariable("styleId") Long styleId, ModelMap map)
    {
        ProStyle proStyle = styleService.selectStyleById(styleId);
        System.out.println(proStyle);
        map.addAttribute("proStyle", proStyle);
        return prefix + "/edit";
    }

    /**
     * 修改产品风格
     * @param proStyle
     * @return
     */
    @RequiresPermissions("pro:style:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ProStyle proStyle)
    {
        if (UserConstants.TYPE_CODE_NOT_UNIQUE.equals(styleService.checkStyleNameUnique(proStyle)))
        {
            return error("修改产品风格'" + proStyle.getStyleName() + "'失败，产品风格名称已存在");
        }

        return toAjax(styleService.updateStyle(proStyle));
    }

    /**
     * 分页查询产品风格
     * @param proStyle 条件查询的条件
     * @return
     */
    @RequiresPermissions("pro:style:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProStyle proStyle){
        startPage();
        List<ProStyle> proStyles = styleService.selectStyle(proStyle);

        return getDataTable(proStyles);
    }
    /**
     * 检查产品风格名称是否唯一
     */
    @PostMapping("/checkStyleNameUnique")

    @ResponseBody
    public String checkTypeNameUnique(ProStyle proStyle)
    {
        return styleService.checkStyleNameUnique(proStyle);
    }

    /**
     * 添加产品风格的操作
     * @param proStyle
     * @return
     */

    @RequiresPermissions("pro:style:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(ProStyle proStyle){

        if (UserConstants.TYPE_CODE_NOT_UNIQUE.equals(styleService.checkStyleNameUnique(proStyle)))
        {
            return error("新增产品风格'" +proStyle.getStyleName() + "'失败，产品风格名称已存在");
        }

        return toAjax(styleService.insertProStyle(proStyle));
    }

    /**
     * 删除公告的操作
     * @param ids
     * @return
     */
    @RequiresPermissions("pro:style:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids){

        try {
            int  result = styleService.removeProStyleById(ids);
            return toAjax(result);
        } catch (CmsNotDeleteException e) {
            return AjaxResult.error(e.getMessage());
        }

        
        

    }
}
